package dao.hibernateDao;

import dao.interfaces.IAlbum;
import hibernate.util.HibernateUtil;
import model.Album;
import org.hibernate.Criteria;
import org.hibernate.Session;

import java.sql.SQLException;
import java.util.List;

/**
 * User: Станислав
 * Date: 17.08.14
 */
public class AlbumHibernateDao implements IAlbum {
    @Override
    public void addAlbum(Album album) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.save(album);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void deleteAlbum(Album album) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.delete(album);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Album> getAllAlbum()
            throws SQLException {
        List<Album> list = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            Criteria query = session.createCriteria(Album.class);
            list = query.list();
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public void echo() {
        System.out.println("AlbumHibernateDao run!");
    }
}
